<template>
  <el-dialog
      v-dialogDrag
      append-to-body
      top="6vh"
      :width="width"
      :close-on-click-modal="false"
      :close-on-press-escape="true"
      :title="'选择学生'"
      :visible.sync="isVisible">
    <el-form size="mini" :inline="true" @keyup.enter.native="getDataList(true)">
      <el-form-item>
        <el-input clearable v-model="queryParams.queryModel.studentName" placeholder="学生姓名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input clearable v-model="queryParams.queryModel.studentId" placeholder="学籍号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList(true)" type="primary" icon="el-icon-search">查询</el-button>
        <el-button @click="resetParams" type="warning" icon="el-icon-delete">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      highlight-current-row
      border
      :header-cell-style="{background:'#ECF5FF',color:'#000',fontWeight:'bold',fontSize:'14px'}"
      :height="tableHeight"
      :data="pageData.list"
      :default-sort = "{prop: 'updateTime', order: 'ascending'}"
      @sort-change="sortChange"
      v-loading="dataListLoading"
      ref="table"
      style="width: 100%;">
      <el-table-column
        sortable
        prop="studentName"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        min-width="70"
        label="学生姓名">
      </el-table-column>
      <el-table-column
        sortable
        prop="studentId"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        label="学籍号">
      </el-table-column>
      <el-table-column
        sortable
        prop="idNumber"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        label="身份证号">
      </el-table-column>
      <el-table-column
        sortable
        prop="schoolName"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        label="学校">
      </el-table-column>
      <el-table-column
        sortable
        prop="className"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        label="班级">
        <template slot-scope="scope">{{ scope.row.gradeName }}{{ scope.row.className }}</template>
      </el-table-column>
      <el-table-column
        sortable
        prop="birthday"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        label="出生日期">
        <template slot-scope="scope">{{ scope.row.birthday | dateformat('YYYY-MM-DD') }}</template>
      </el-table-column>
      <el-table-column
        sortable
        prop="sex"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        min-width="50"
        label="性别">
        <template slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.sex == '1'">男</el-tag>
          <el-tag size="mini" v-if="scope.row.sex == '0'" type="danger">女</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        min-width="50"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" style="padding:0;" @click="selectHandle(scope.row)">选中</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="queryParams.pageNo"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="queryParams.pageSize"
      :total="pageData.totalCount"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="isVisible = false">关闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { initQueryParams, getScreenWidth } from '@/utils'

  export default {
    data () {
      return {
        dialog: {
          isVisible: false,
          type: 'add'
        },
        width: getScreenWidth(),
        tableHeight: this.$store.state.common.documentClientHeight - 320,
        queryParams: initQueryParams(),
        pageData: {
          totalCount: 0,
          list: [],
          extData: {}
        },
        dataListLoading: false,
        btnLoading: false
      }
    },
    props: {
      dialogVisible: {
        type: Boolean,
        default: false
      }
    },
    components: { },
    computed: {
      isVisible: {
        get() {
          return this.dialogVisible
        },
        set() {
          this.close()
          this.reset()
        }
      }
    },
    activated () {
      this.getDataList(true)
    },
    updated () {
      this.$nextTick(() => {
        this.$refs['table'].doLayout()
      })
    },
    methods: {
      editClose() {
        this.dialog.isVisible = false
      },
      editSuccess() {
        this.getDataList(true)
      },
      resetParams() {
        this.queryParams = initQueryParams()
        this.getDataList(true)
      },
      // 获取数据列表
      getDataList (isReload) {
        if (isReload) {
          this.queryParams.pageNo = 1
        }
        this.dataListLoading = true
        this.$http({
          url: this.$http.requestUrl('/stu/student/list'),
          method: 'get',
          params: this.$http.requestParams(this.queryParams)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.pageData = data.page
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.queryParams.pageSize = val
        this.queryParams.pageNo = 1
        this.getDataList(false)
      },
      // 当前页
      currentChangeHandle (val) {
        this.queryParams.pageNo = val
        this.getDataList(false)
      },
      // 排序
      sortChange: function (column) {
        if (column.order === 'descending') {
          this.queryParams.sort = column.prop + ' desc'
        } else if (column.order === 'ascending') {
          this.queryParams.sort = column.prop + ' asc'
        }
        this.getDataList(true)
      },
      // 选择学生
      selectHandle (row) {
        this.$emit('success', row)
        this.isVisible = false
      },
      close() {
        this.$emit('close')
      },
      reset() {
        this.pageData = {
          totalCount: 0,
          list: [],
          extData: {}
        }
      }
    }
  }
</script>
